#!/usr/bin/env python
# -*- coding:utf-8 -*-

from flask_restful import Resource, reqparse
from flask import jsonify

from service.wordbookService import WordbookService
from controller.wordbookController import WordbookController
from utils import commons
from utils.response_code import RET


class WordbookOtherResource(Resource):

    @classmethod
    def sensitive_update(cls, wordbook_id):
        if not wordbook_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('is_delete', location='form', required=False, help='is_delete参数类型不正确或缺失')
        
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['wordbook_id'] = wordbook_id

        res = WordbookController.update(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def create_wordbook(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', type=str,location='form', required=True, help='用户ID不能为空')
        parser.add_argument('wordbook_name', type=str,location='form', required=True, help='单词本名称不能为空')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = WordbookService.create_wordbook(kwargs['user_id'], kwargs['wordbook_name'])

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def add_word_to_wordbook(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', type=str,location='form', required=True, help='用户ID不能为空')
        parser.add_argument('word_id', type=str, location='form', required=True, help='单词ID不能为空')
        parser.add_argument('wordbook_id', type=str,location='form', required=True, help='单词本名称不能为空')
        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = WordbookService.add_word_to_wordbook(kwargs['user_id'], kwargs['word_id'], kwargs['wordbook_id'])

        return jsonify(code=res['code'], message=res['message'], data=res['data'])

    @classmethod
    def get_sorted_words(cls, wordbook_id):
        if not wordbook_id:
            return jsonify(code=RET.NODATA, message='primary key missed', error='primary key missed')

        parser = reqparse.RequestParser()
        parser.add_argument('reverse', type=bool, location='args', required=False, default=False, help='是否逆序')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)
        kwargs['wordbook_id'] = wordbook_id

        res = WordbookService.get_words_by_wordbook_id(**kwargs)

        return jsonify(code=res['code'], message=res['message'], data=res['data'])


    @classmethod
    def get_user_wordbooks(cls):
        parser = reqparse.RequestParser()
        parser.add_argument('user_id', type=str, location='form', required=True, help='用户ID不能为空')

        kwargs = parser.parse_args()
        kwargs = commons.put_remove_none(**kwargs)

        res = WordbookService.get_wordbooks_by_user_id(kwargs['user_id'])
        return jsonify(code=res['code'], message=res['message'], data=res['data'])